package com.oscarmovie.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.oscarmovie.Constants;
import com.oscarmovie.model.Mpr;
import com.oscarmovie.model.Person;
import com.oscarmovie.model.Role;
import com.oscarmovie.service.MprService;
import com.oscarmovie.service.PersonService;
import com.oscarmovie.service.RoleService;

public class MprAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private MprService mprService;
	
	private PersonService personService;
	private RoleService roleService;
	private Mpr mpr;
	private List<Mpr> mprs;
	
	private List<Person> persons;
	private List<Role> roles;
	
	private Person person;//for search keyword;
	
	public String execute() throws Exception {
		
		throw new Exception("Please specify method name!");
	}
	public String add() throws Exception {
		setMpr(null);
		return Constants.ADD;
	}
	public String edit() throws Exception {
		loadAll();
		load();
		return Constants.EDIT;
	}
	public String save() throws Exception {
		mprService.save(mpr);
		return edit();
	}

	public String load() throws Exception {
		this.setMpr(mprService.load(mpr.getId()));
		return SUCCESS;
	}
	
	public String loadAll() throws Exception {
		this.mprs = mprService.loadAll();
		
		this.persons = personService.loadAll();
		this.roles = roleService.loadAll();
		
		return SUCCESS;
	}
	public String delete() throws Exception {
		mprService.delete(mpr);
		return "success";
	}
	public String select() throws Exception {
		
		this.roles = roleService.loadAll();
		
		if(person!=null && !"".equals(person.getName())){
			Person p = new Person();
			String n = person.getName().trim();
			p.setName(n);
			p.setNameCn(n);
			
			persons = personService.loadAll(p);
		} else {
			persons = personService.loadAll();
		}
		
		return Constants.SELECT;
	}
	public List<Mpr> getMprs() {
		return mprs;
	}

	public void setMprService(MprService mprService) {
		this.mprService = mprService;
	}

	public void setMpr(Mpr mpr) {
		this.mpr = mpr;
	}

	public Mpr getMpr() {
		return mpr;
	}
	
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	public PersonService getPersonService() {
		return personService;
	}
	
	public List<Role> getRoles() {
		return roles;
	}
	public void setPersons(List<Person> persons) {
		this.persons = persons;
	}
	public List<Person> getPersons() {
		return persons;
	}
	
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	public RoleService getRoleService() {
		return roleService;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	public Person getPerson() {
		return person;
	}

}
